Memory management among processes is an important task supported by most modern operating systems. The task of memory management involves several aspects. For example, virtual memory is an important aspect of memory management. Virtual memory essentially provides an alternate set of memory addresses. Processes use virtual addresses rather than physical addresses to store instructions and data. When utilized by a process, the virtual addresses are converted into physical memory addresses.
The basic purpose of virtual memory is to enlarge the address space a process can utilize. For example, virtual memory might contain twice as many addresses as physical memory. A process using all of its virtual memory, therefore, would not be able to fit in physical memory all at once. Nevertheless, the computer could execute such a process by copying into physical memory those portions of the process needed at any given point during process execution.
Another aspect involved in memory management is paging, which facilitates the use of the enlarged address space provided by virtual memory. Where paging is implemented, the operating system divides memory into pages, each of which contains a fixed number of addresses, and moves pages from physical memory to disk while not actively in use by an executing process (this is practical because the storage capacity of disk is typically much larger than the storage capacity of physical memory). Each page moved to disk is stored on disk until it is needed by an executing process. When the page is needed, the operating system copies it from disk to physical memory. By use of the techniques of virtual memory and paging, each process can execute in its own virtual memory space that is apparently much larger than would be possible if the computer system were constrained to using only physical memory without paging.
In addition to increasing the effective size of memory, virtual memory substantially improves the efficiency of physical memory utilization. Specifically, physical addresses only need to be assigned to those virtual addresses of a process that are currently active, such as, those virtual addresses that have been made active to store data into an allocated data structure, or to store a call stack frame. If a process is not using a portion of its assigned virtual address space, then no physical addresses need be reserved for these unused virtual addresses. As a result, a larger portion of physical memory is available for use by other processes which can continue to operate as if they also have a large contiguous available address space.
Another aspect involved in memory management is memory allocation within a process. Processes executing on, for example, Unix-like operating systems use “malloc( )” and its associated family of routines to allocate memory for data structures, and subsequently to free such memory. When called from within a process, malloc( ) requests a block of free memory of a particular size from the operating system. A block may be a portion of a page. In one case, the request can be satisfied by returning to the process the block of memory of the requested size from within a page of physical memory already allocated to the process. However, in another case, if the requested size is larger, then the operating system may have to allocate to the process a page of physical memory not currently allocated to the process, and then return to the process a block of memory of the requested size from within the newly allocated page.
When a malloc( ) family routine is called from within a process to free a block of memory, the operating system receives the request and de-allocates the block of memory associated with the request. If the de-allocation results in a physical page of memory that is entirely unused by the process, such that no blocks are allocated from within the physical page of memory, then the physical page can be de-allocated from the process, for later allocation to other processes.